<?php
/*
 * Prompt
 * @package    pre
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: f8761affb59cf08136aae72bfb1bea8f396a46f8 $
 */

/**
 * Class Prompt
 */
class Prompt {

	/**
	 * 前台无权访问页面
	 */
	public static function noPermission() {
		$head_title = lang('message->without_permission');
		include template('noright', 'message');
		exit();
	}

	/**
	 * 分类/列表 不存在
	 */
	public static function listNotFound() {
		$head_title = lang('message->cate_not_exists');
		Header::code404();
		include template('list-notfound', 'message');
		exit();
	}

	/**
	 * 404 错误
	 */
	public static function error404() {
		$head_title = '文件不存在!';
		Header::code404();
		include template('404', 'message');
		exit();
	}

	/**
	 * alert 警告信息
	 * @param string $dmessage
	 * @param string $dforward
	 * @param string $extend
	 */
	public static function alert($dmessage = errmsg, $dforward = '', $extend = '') {
		global $DT;
		include template('alert', 'message');
		exit();
	}

	/**
	 * 提示信息
	 * @param string $dmessage
	 * @param string $dforward
	 * @param string $dtime
	 */
	public static function message($dmessage = ERR_MSG, $dforward = '', $dtime = '3') {
		if (!$dmessage && $dforward && $dforward != 'goback') dheader($dforward);
		include template('message', 'message');
		exit();
	}

	/**
	 * 提示信息
	 * @param string $dmessage
	 * @param string $dforward
	 * @param string $dtime
	 */
	public static function returnMessage($type = 'message', $dmessage = ERR_MSG, $dforward = '', $dtime = '3') {

		if ($type == 'message') {
			if (!$dmessage && $dforward && $dforward != 'goback') dheader($dforward);
			include template('message', 'message');
		} elseif ($type == 'json') {
			if (!$dmessage || preg_match('/<script.+<\/script>/', $dmessage)) {
				$json = array("errorNo" => 1, "errorContent" => "");
			} else {
				$json = array("errorNo" => 0, "errorContent" => $dmessage);
			}
			echo json_encode($json);
		}
		exit();

	}

	/**
	 * 调用函数显示右上角提示.
	 * @param string $msg
	 * @param string $forward
	 */
	public static function tip($msg = '', $forward = '') {
		if (!$msg && !$forward) {
			$msg = Cookie::get('dmsg');
			if ($msg) {
				$script = <<<SCRIPT
				<script defer="defer">
				seajs.use(['easyui', 'easyui-css'], function(){
					 $.messager.show({
		                title:'通知',
		                msg:'$msg.',
		                timeout:2000,
		                showType:'slide'
		            });
				})
				</script>
SCRIPT;
				echo $script;
				Cookie::set('dmsg', '');
			}
		} else {
			Cookie::set('dmsg', $msg);
			$forward = preg_replace("/(.*)([&?]rand=[0-9]*)(.*)/i", "\\1\\3", $forward);
			$forward = str_replace('.php&', '.php?', $forward);
			$forward = strpos($forward, '?') === false ? $forward . '?rand=' . mt_rand(10, 99) : str_replace('?', '?rand=' . mt_rand(10, 99) . '&', $forward);
			Header::location($forward);
		}
	}
}